package cn.itcast.nems.base.web.controller.b;

import cn.itcast.nems.base.api.DiscountsApi;
import cn.itcast.nems.base.dto.DiscountQueryDTO;
import cn.itcast.nems.base.dto.DiscountTotalDTO;
import cn.itcast.nems.base.service.DiscountsQueryService;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
/**
 * <p>
 * 优惠项查询
 * </p>
 *
 * @author zhouwei
 * @since 2023/12/8 5:36 PM
 **/
@RestController
@RequestMapping("/b")
@RequiredArgsConstructor
@Slf4j
public class DiscountController {
    private final DiscountsApi discountsApi ;
    private final DiscountsQueryService discountsQueryService;

    /**
     * 根据班级ID 查询优惠项
     *
     * @param clazzId 班级ID
     * @return 优惠项
     */
    @PostMapping("/v3/{clazz_id}/discounts")
    public ResponseEntity<DiscountTotalDTO> findV3DiscountsByCondition(
                                                                        @PathVariable("clazz_id") Integer clazzId,
                                                                        @RequestBody(required = false) DiscountQueryDTO discountQueryDTO) {
        log.info("【根据班级ID 查询优惠项】 请求参数clazzId：{}，，discountQueryDTO：{}", clazzId, JSON.toJSONString(discountQueryDTO));
        discountQueryDTO   =  Objects.requireNonNullElse(discountQueryDTO ,new DiscountQueryDTO()) ;
        discountQueryDTO.setClazzId(clazzId);
        return ResponseEntity.ok().body(discountsQueryService.findChildListAndCalculateDiscountAmount(discountQueryDTO));

    }

    /**
     * 根据班级ID 查询优惠项
     *
     * @param clazzId 班级ID
     * @return 优惠项
     */
    @PostMapping(value = "/v3/{clazz_id}/{parent_id}/discounts")
    public ResponseEntity<DiscountTotalDTO> findV3DiscountsByCondition(
                                                                        @PathVariable("clazz_id") Integer clazzId,
                                                                        @PathVariable(value = "parent_id",required = false) Integer parentId,
                                                                        @RequestBody(required = false) DiscountQueryDTO discountQueryDTO) {
        log.info("【根据班级ID 父节点ID 查询优惠项】 请求参数clazzId：{}, parentId：{},discountQueryDTO：{} ", clazzId,parentId,JSON.toJSONString(discountQueryDTO));
        discountQueryDTO   =  Objects.requireNonNullElse(discountQueryDTO ,new DiscountQueryDTO()) ;
        discountQueryDTO.setClazzId(clazzId);
        discountQueryDTO.setParentId(Objects.isNull(parentId) || parentId ==0 ?null: parentId);
        return ResponseEntity.ok().body(discountsQueryService.findChildListAndCalculateDiscountAmount(discountQueryDTO));
    }

}
